package abel.wlmr.model.type;

import abel.wlmr.model.WlmrException;
import abel.wlmr.model.WlmrField;

public class DecFormat extends WlmrField {

    /** [5,8] */
    private byte intWidth = 5;

    /** [1,4] */
    private byte fracWidth = 1;

    public DecFormat() {
        super(1);
    }

    @Override
    public int getValue() {
        return (intWidth - 5) << 2 | (fracWidth - 1);
    }

    @Override
    public void parseValue(int v) throws WlmrException {
        intWidth = (byte) (v & 0x0C + 5);
        fracWidth = (byte) (v & 0x03 + 1);
    }

    /* (non-Javadoc)
     * @see java.lang.Object#toString()
     */
    @Override
    public String toString() {
        return String.format("{intWidth:%s, fracWidth:%s}", intWidth, fracWidth);
    }

    /**
     * @return the intWidth
     */
    public byte getIntWidth() {
        return intWidth;
    }

    /**
     * [5,8]
     * 
     * @param w
     *            the intWidth to set
     */
    public void setIntWidth(byte w) {
        if (w < 5 || w > 8) {
            throw new IllegalArgumentException("int width should be [5,8] but: " + w);
        }

        this.intWidth = w;
    }

    /**
     * @return the fracWidth
     */
    public byte getFracWidth() {
        return fracWidth;
    }

    /**
     * [1,4]
     * 
     * @param w
     *            the fracWidth to set
     */
    public void setFracWidth(byte w) {
        if (w < 1 || w > 4) {
            throw new IllegalArgumentException("fraction width should be [1,4] but: " + w);
        }

        this.fracWidth = w;
    }

}
